package com.test.drawing;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import com.test.loading.Frame;
import com.test.loading.Scene;

/**
 * AnimationBuffer<br>
 * Holds all buffers needed for an OpenGL Animation
 */
public class AnimationBuffer extends MeshBuffer {

	private FloatBuffer[] frames;
	private int frameToShow;

	/**
	 * AnimationBuffer Constructor
	 * 
	 * @param scene
	 *            Scene to be used this AnimationBuffer<br>
	 *            Vertices, Indices, Textures and Normals are taken from the
	 *            Scene
	 */
	public AnimationBuffer(Scene scene) {
		super(scene);

		Frame[] framesArray = scene.getFrames();

		frames = new FloatBuffer[framesArray.length];

		ByteBuffer vbb;

		for (int i = 0; i < scene.getFramesSize(); i++) {

			vbb = ByteBuffer.allocateDirect(framesArray[i].getVertices().length
					* FLOAT_BYTE_SIZE);

			vbb.order(ByteOrder.nativeOrder());
			frames[i] = vbb.asFloatBuffer();
			frames[i].put(framesArray[i].getVertices());
			frames[i].position(0);
		}
	}

	@Override
	public FloatBuffer getVerticesBuffer() {
		int frameToReturn = frameToShow;
		frameToShow = 0;
		return frames[frameToReturn];
	}

	/**
	 * Set the Next Buffer to Get with getVerticesBuffer() to the given position
	 * 
	 * @param pos
	 *            Position to be Setted as Next Frame
	 */
	public void setNextFrame(int pos) {
		frameToShow = pos;
	}

	/**
	 * Gets the Frames Buffers' Array Size
	 * 
	 * @return int - Frames Buffer's Array Size
	 */
	public int getFramesBufferSize() {
		return frames.length;
	}
}
